package org.reliablesource.prism.ui.adapters;

/**
 *
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Mar 11, 2006
 *	@author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

import java.util.HashMap;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.ui.views.properties.IPropertySource;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;

/**
 *
 *	Created on: Feb 25, 2006
 *  @author Dennis Park 
 *  	<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

public class PrismElementAdapterFactory implements IAdapterFactory{

	private static PrismElementAdapterFactory instance; 
	private final static Class[] adapterList = { IPropertySource.class, IAdaptable.class };
	private static HashMap unitAdapterMap;
	private static HashMap nodeAdapterMap;
	
	static{
		unitAdapterMap = new HashMap();
		nodeAdapterMap = new HashMap();
		instance = new PrismElementAdapterFactory();
	}
	
	private PrismElementAdapterFactory(){
		
	}
	
	public static PrismElementAdapterFactory getInstance(){
		return instance;
	}
	
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		Object o;

		if (adaptableObject instanceof IPrismTutorial){
			
			if(nodeAdapterMap.containsKey(String.valueOf(adaptableObject.hashCode())))
				o = nodeAdapterMap.get(String.valueOf(adaptableObject.hashCode()));
			else{
				o = new PrismNodeAdapter((IPrismTutorial) adaptableObject);		
				nodeAdapterMap.put(String.valueOf(adaptableObject.hashCode()), o);
			}
			return o;
		}
		else if (adaptableObject instanceof IPrismScreen){
			if(adapterType.getName().equals(IPropertySource.class.getName())){			
				if(unitAdapterMap.containsKey(String.valueOf(adaptableObject.hashCode())))
					o = unitAdapterMap.get(String.valueOf(adaptableObject.hashCode()));
				else{
					o = new PrismScreenPropertySourceAdapter((IPrismScreen) adaptableObject);			
					unitAdapterMap.put(String.valueOf(adaptableObject.hashCode()), o);
				}
				return o;
			}
//			
//			if(unitAdapterMap.containsKey(String.valueOf(adaptableObject.hashCode())))
//				o = unitAdapterMap.get(String.valueOf(adaptableObject.hashCode()));
//			else{
//				o = new PrismUnitAdapter((IPrismScreen) adaptableObject);	
//				unitAdapterMap.put(String.valueOf(adaptableObject.hashCode()), o);
//			}
//			return o;
		}
		return null;
	}

	public Class[] getAdapterList() {
		return adapterList;
	}
}
